package com.lenovo.smarttraffic.ui.activity.stage2;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.lenovo.smarttraffic.MainActivity;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.database.BalanceByCarNumber;
import com.lenovo.smarttraffic.database.CarInfoBean;
import com.lenovo.smarttraffic.database.RechargeRecord;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;
import com.lenovo.smarttraffic.util.Utils;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ETCAccountActivity extends BaseActivity {

    private TextView money_tv;

    private Spinner car_sp;
    private Button find_btn;
    private TextView find_success;

    private Spinner money_sp;
    private Button charge_btn;
    private TextView charge_success;

    private Spinner car_id_sp;
    private RadioButton time_up;
    private RadioButton time_down;
    private Button find_btn_2;

    private ListView mListView;


    @Override
    protected int getLayout() {
        return R.layout.activity_etc_account;
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
        initData();
    }

    public void initView() {
        Toolbar mToolbar = findViewById(R.id.toolbar);
        mToolbar.setNavigationOnClickListener(view -> finish());


        money_tv = findViewById(R.id.money_tv);

        car_sp = findViewById(R.id.car_sp);
        find_btn = findViewById(R.id.find_btn);
        find_btn.setOnClickListener(v -> {
            List<CarInfoBean> rowsdetailBeans = LitePal.where("number = ?", car_sp.getSelectedItem().toString()).find(CarInfoBean.class);
            if (rowsdetailBeans.size() == 1) {
                String carnumber = rowsdetailBeans.get(0).getCarnumber();
                //对数据库进行修改来充值
                BalanceByCarNumber balanceByCarNumber = LitePal.where("carNumber = ?", carnumber).find(BalanceByCarNumber.class).get(0);
                int howMoney = 0;
                if (!balanceByCarNumber.getBalance().isEmpty()) {
                    howMoney = Integer.valueOf(balanceByCarNumber.getBalance());
                }
                money_tv.setText("账户余额:" + howMoney + "元");
                find_success.setVisibility(View.VISIBLE);
            } else {
                money_tv.setText("查询出错");
                find_success.setVisibility(View.INVISIBLE);
            }
        });
        find_success = findViewById(R.id.find_success);

        money_sp = findViewById(R.id.money_sp);
        charge_btn = findViewById(R.id.charge_btn);
        charge_btn.setOnClickListener(v -> doCharge());
        charge_success = findViewById(R.id.charge_success);

        car_id_sp = findViewById(R.id.car_id_sp);
        time_up = findViewById(R.id.time_up);
        time_down = findViewById(R.id.time_down);
        find_btn_2 = findViewById(R.id.find_btn2);
        find_btn_2.setOnClickListener(v -> doFindAndMakeTable());


        mListView = findViewById(R.id.listview_car_charge);
    }


    public void initData() {

    }


    private void doCharge() {
        //读取spinner当前的选择
        String accountBalanceText = money_sp.getSelectedItem().toString();
        String accountCarnumberText = car_sp.getSelectedItem().toString();
        //根据车号查找所有的信息
        List<CarInfoBean> rowsdetailBeans = LitePal.where("number = ?", accountCarnumberText).find(CarInfoBean.class);
        if (rowsdetailBeans.size() == 1) {
            String carnumber = rowsdetailBeans.get(0).getCarnumber();
            //对数据库进行修改来充值
            BalanceByCarNumber balanceByCarNumber = LitePal.where("carNumber = ?", carnumber).find(BalanceByCarNumber.class).get(0);
            int howMoney = 0;
            if (!balanceByCarNumber.getBalance().isEmpty()) {
                howMoney = Utils.parseInt(balanceByCarNumber.getBalance());
            }
            String over = String.valueOf(howMoney + Utils.parseInt(accountBalanceText));
            balanceByCarNumber.setBalance(over);
            balanceByCarNumber.save();
            //记录充值记录
            new RechargeRecord(new Date(), carnumber, accountBalanceText,
                    over, MainActivity.userName).save();
            //提示充值成功。刷新账户余额
            money_tv.setText("充值成功，余额 " + over);
            Toast.makeText(this, "充值成功", Toast.LENGTH_SHORT).show();
            money_tv.setVisibility(View.VISIBLE);
        } else {
            Toast.makeText(this, "没有该车辆", Toast.LENGTH_SHORT).show();
            money_tv.setVisibility(View.INVISIBLE);
        }
    }


    private void doFindAndMakeTable() {
        //根据车号查出车牌号
        String accountCarnumberText = car_id_sp.getSelectedItem().toString();
        //查找车牌号
        CarInfoBean rowsdetailBean = LitePal.where("number = ?", accountCarnumberText).find(CarInfoBean.class).get(0);
        if (rowsdetailBean != null) {
            String carnumber = rowsdetailBean.getCarnumber();
            //获得排序顺序
            List<RechargeRecord> rechargeRecords = new ArrayList<>();
            if (time_down.isChecked()) {
                rechargeRecords = LitePal.where("carNumber = ?", carnumber).order("date asc").find(RechargeRecord.class);
            }
            if (time_up.isChecked()) {
                rechargeRecords = LitePal.where("carNumber = ?", carnumber).order("date desc").find(RechargeRecord.class);
            }

            if (rechargeRecords.size() == 0) {
                Toast.makeText(this, "车辆没有充值记录", Toast.LENGTH_SHORT).show();
            } else {
                //mListView.setAdapter(new AdapterListView(rechargeRecords, this));
            }
        } else {
            Toast.makeText(this, "没有该车辆", Toast.LENGTH_SHORT).show();
        }
    }
}
